package robotinterface.list;

import java.io.*;
import java.util.ArrayList;
import javax.swing.AbstractListModel;
import robotinterface.list.entry.*;

/**
 *
 * @author Brian Bailey
 */
public class RobotListModel<T> extends AbstractListModel {

    private final EntryList<Entry> list;

    public RobotListModel(ArrayList l) {
        list = new EntryList<>();
        list.addAll(l);
    }

    public Entry stepForward() {
        Entry step = list.stepForward();
        list.incIndex();
        return step;
    }

    public Entry stepBackward() {
        Entry step = list.stepBackward();
        list.decIndex();
        return step;
    }

    public int getIndex() {
        return list.getIndex();
    }

    public void setIndex(int index) {
        list.setIndex(index);
    }

    /**
     * Set Index to value to index of canceled task
     */
    public void cancelled() {
        list.decIndex();
    }

    @Override
    public int getSize() {
        return list.size();
    }

    @Override
    public Object getElementAt(int index) {
        return list.get(index);
    }

    public void add(Entry entry) {
        list.add(entry);
    }

    public void set(int index, Entry entry) {
        list.set(index, entry);
    }

    public void addElementAt(int index, Entry entry) {
        list.add(index, entry);
    }

    public void removeElementAt(int index) {
        list.remove(index);
    }

    public void readObject(String varName)
            throws IOException, ClassNotFoundException 
    {
        try (FileInputStream fileIn = new FileInputStream(varName);
                ObjectInputStream in = new ObjectInputStream(fileIn)) {
            EntryList<Entry> e = (EntryList<Entry>) in.readObject();
        }
    }

    public void writeObject(String varName)
            throws IOException
    {
        try (FileOutputStream fileOut = new FileOutputStream(varName);
                ObjectOutputStream out = new ObjectOutputStream(fileOut)) {
            out.writeObject(list);
        }
    }
}
